package com.md.rps.service;

import com.md.rps.mybatis.interfaces.I_REGISTER;
import com.md.rps.mybatis.model.REGISTER;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.scheduling.annotation.Async;

import java.util.concurrent.Callable;

public class RegisterThread implements Callable<Integer> {
    private static Logger logger = LogManager.getLogger(RegisterThread.class);
    private I_REGISTER i_register;
    private REGISTER register;

    public RegisterThread(I_REGISTER i_register, REGISTER register) {
        this.i_register = i_register;
        this.register = register;
    }

    /**
     * Computes a result, or throws an exception if unable to do so.
     *
     * @return computed result
     * @throws Exception if unable to compute a result
     */
    @Override
    @Async("asyncServiceExecutor")
    public Integer call() throws Exception {
        Integer result = 0;
        synchronized (RegisterLock.registerLock) {
            logger.info("执行线程：名称--->"+Thread.currentThread().getName());
            Integer num = i_register.getMaxOrder(register.getDOCTOR_ID(),register.getRESERVE_TIME(),
                    register.getTIME_OF_DAY());
            Integer isRegisted = i_register.isRegisted(register.getDOCTOR_ID(), register.getUSER_ID(),
                    register.getRESERVE_TIME(),register.getTIME_OF_DAY());
            System.out.println(num);
            if(num <= 20 && num !=null && num!=0 && isRegisted<1) {
                register.setORDER(num+1);
                result =  i_register.addRegister(register);
            }
        }
        return result;
    }
}
